package cloud.seri.iot.manager.service

import cloud.seri.iot.manager.domain.Commodity
import cloud.seri.iot.manager.repository.CommodityRepository
import cloud.seri.iot.manager.service.dto.CommodityDTO
import cloud.seri.iot.manager.service.mapper.CommodityMapper
import org.slf4j.LoggerFactory

import org.springframework.stereotype.Service

import java.util.Optional

/**
 * Service Implementation for managing [Commodity].
 */
@Service
class CommodityService(
    val commodityRepository: CommodityRepository,
    val commodityMapper: CommodityMapper
) {

    private val log = LoggerFactory.getLogger(CommodityService::class.java)

    /**
     * Save a commodity.
     *
     * @param commodityDTO the entity to save.
     * @return the persisted entity.
     */
    fun save(commodityDTO: CommodityDTO): CommodityDTO {
        log.debug("Request to save Commodity : {}", commodityDTO)

        var commodity = commodityMapper.toEntity(commodityDTO)
        commodity = commodityRepository.save(commodity)
        return commodityMapper.toDto(commodity)
    }

    /**
     * Get all the commodities.
     *
     * @return the list of entities.
     */
    fun findAll(): MutableList<CommodityDTO> {
        log.debug("Request to get all Commodities")
        return commodityRepository.findAll()
            .mapTo(mutableListOf(), commodityMapper::toDto)
    }

    /**
     * Get one commodity by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    fun findOne(id: String): Optional<CommodityDTO> {
        log.debug("Request to get Commodity : {}", id)
        return commodityRepository.findById(id)
            .map(commodityMapper::toDto)
    }

    /**
     * Delete the commodity by id.
     *
     * @param id the id of the entity.
     */
    fun delete(id: String) {
        log.debug("Request to delete Commodity : {}", id)

        commodityRepository.deleteById(id)
    }
}
